﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JaernCloud.Language.Compiler.Ast;
using JaernCloud.Language.Compiler.Types.Builtin;

namespace JaernCloud.Language.Compiler.Types {
    
    /// <summary>
    /// Node selector which returns a single or 
    /// </summary>
    public class NodeSelector : INodeSelector {

        public IEnumerable<INodeType> Select(INodeType context, string selector) {
            // yeild return etc.
            throw new NotImplementedException();
        }

        public TNodeType Single<TNodeType>(INodeType context, string selector) where TNodeType : INodeType {
            if (context == null) {
                throw new ArgumentException("Context must be non-null");
            }
            if (String.IsNullOrWhiteSpace(selector)) {
                throw new ArgumentException("Selector must be non-null, non-empty and contain non-whitespace characters");
            }
            // TODO: Use parser here for more advanced selector support (see http://api.jquery.com/category/selectors/)
            string[] patterns = selector.Replace("-> ", "->").Split(' ');
            INodeType currentContext = context;
            foreach (string pattern in patterns) {
                if (currentContext is IViewNodeType) {
                    // see if the view inherits from the requested page
                    bool foundPage = false;
                    foreach (INodeType inherited in currentContext.InheritsFrom) {
                        if (inherited.MatchesTag(AstTag.FromTag(pattern))) {
                            foundPage = true;
                            break;
                        }
                    }
                    if (foundPage) {
                        // continue to next pattern
                        continue;
                    }
                }
                bool match = false;
                string tag = pattern;
                bool isAction = tag.StartsWith(ActionNodeType.ACTION_PREFIX);
                if (isAction) {
                    tag = pattern.TrimStart(ActionNodeType.ACTION_PREFIX.ToCharArray());
                }
                foreach (INodeType child in currentContext.Children) {
                    if (child.MatchesTag(AstTag.FromTag(tag))) {
                        // TODO should use recursion to traverse all canidates at each level
                        if (isAction && !(child is IActionNodeType)) {
                            continue;
                        }
                        currentContext = child;
                        match = true;
                        break;
                    }
                }
                if (!match) {
                    return default(TNodeType);
                }
            }
            return (TNodeType)currentContext;
        }
    }
}
